package com.llh.controller.http;

import com.llh.bean.Daily;
import com.llh.bean.MiddleData;
import com.llh.controller.http.common.CommonExcuteTrade;
import com.llh.mapper.DailyMapper;
import com.llh.mapper.MiddleDataMapper;
import com.llh.util.SqlSessionUtil;
import lombok.Data;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
@Data
public class DailyTrade extends CommonExcuteTrade{
    private MiddleData middleData;
    static Logger logger = LogManager.getLogger(DailyTrade.class.getName());
    @Override
    public void parseParam(String[][] items, String[] fields) throws Exception {
        //获取sqlSession
        List<Daily> list = ( List<Daily>)copyProperty(items, fields);
        for (Daily daily : list) {
            daily.setOpen_day(middleData.getOpen_day());
        }
        SqlSession sqlSesion = SqlSessionUtil.getSqlSesion();
        DailyMapper mapper = sqlSesion.getMapper(DailyMapper.class);
        logger.info("插入数据");
        mapper.insertBatch(list);
        logger.info("插入操作成功");
        sqlSesion.close();
        //记录中间表的total字段
        middleData.setTotal(list.size());
        SqlSession sesion = SqlSessionUtil.getSqlSesion();
        MiddleDataMapper middleDataMapper = sesion.getMapper(MiddleDataMapper.class);
        middleDataMapper.updateTotal(middleData);
        logger.info("记录中间表成功");
        sesion.close();


    }

    @Override
    public List copyProperty(String[][] items, String[] fields) throws Exception {
        List<Daily> dailyList = new LinkedList<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        for (int j = 0; j < items.length; j++) {
            Daily daily = new Daily();
            Field[] declaredFields = daily.getClass().getDeclaredFields();
            //参数值数组
            String[] item = items[j];
            //数据值校验
            if(item.length!=fields.length){
                throw new Exception("响应的数据有误");
            }
            for (int i = 0; i < fields.length; i++) {
                //参数名
                String field = fields[i];
                //参数值
                String value = item[i];
                for (Field declaredField : declaredFields) {
                    declaredField.setAccessible(true);
                    String fieldName = declaredField.getName();
                    if(field.equals(fieldName)){
                        //将字符串转换成date时间类型
                        if(field.equals("trade_date")){
                            //将字符串转换成date.并赋值
                            if((!(value==null))&&(!value.equals(""))&&(!value.equals("null"))){
                                declaredField.set(daily,format.parse(value));
                            }
                        //将字符串装换成double
                        }else if(!field.equals("ts_code")){
                            if((!(value==null))&&(!value.equals(""))&&(!value.equals("null"))){
                                declaredField.set(daily,Double.valueOf(value));
                            }
                            //直接赋值为字符串
                        }else{
                            if((!(value==null))&&(!value.equals(""))&&(!value.equals("null"))){
                                declaredField.set(daily,value);
                            }
                        }

                    }
                }
            }
            //添加到list集合
           dailyList.add(daily);

        }
        return dailyList;
    }
}
